package personnel.department;

import static personnel.department.dao.HibernateUtils.getDAO;

import java.lang.reflect.InvocationTargetException;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.dialogs.ProgressMonitorDialog;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.mihalis.opal.infinitePanel.InfiniteProgressPanel;

import personnel.department.dao.HibernateUtils;
import personnel.department.pages.MainPage;
import personnel.department.pages.constants.Icons;


public class PersonnelDepartmentMain {

	private static FormToolkit toolkit;

	public static void main(String[] args) {
		final Display display = new Display();
		toolkit = new FormToolkit(display);
		Shell shell = new Shell(display);
		Thread thread = createStartPanel(shell);
		thread.start();
		
		createMainPage(shell);
		shell.open();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) 
				display.sleep();
		}
		display.dispose();
		HibernateUtils.close();
	}

	private static Thread createStartPanel(Shell shell) {
		InfiniteProgressPanel panel = InfiniteProgressPanel.getInfiniteProgressPanelFor(shell);
		panel.setText("Выполняется открытие приложения...");
		panel.setTextColor(shell.getDisplay().getSystemColor(SWT.COLOR_DARK_RED));
		panel.setTextFont(new Font(shell.getDisplay(), "Lucida Sans", 18, SWT.BOLD));
		panel.start();
		Thread thread = new Thread(()-> {
			getDAO(); 
			panel.stop();
		});
		return thread;
	}

	private static void createMainPage(Shell shell) {
		shell.setImage(new Image(shell.getDisplay(), Icons.MAIN_ICON.getPath()));
		shell.setText("Рабочее место \"КАДРЫ\"");
		new MainPage(shell);
	}

	public static FormToolkit getToolkit() {
		return toolkit;
	}

}
